/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ep.m001.ui.controllers;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.GenericType;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.json.JSONConfiguration;
import com.sun.jersey.core.util.MultivaluedMapImpl;
import com.vaadin.data.Item;
import com.vaadin.data.util.BeanItem;
import com.vaadin.terminal.gwt.server.WebApplicationContext;
import ep.libs.interfaces.IModule;
import ep.persistence.entities.RegisterDoctorBean;
import ep.persistence.entities.RegisterPharmacistBean;
import ep.persistence.entities.T001User;
import ep.persistence.entities.T002Doctor;
import ep.persistence.entities.T003Pharmacist;
import ep.persistence.entities.T004Insurance;
import java.util.Collection;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.UriBuilder;

/**
 *
 * @author Spyros
 */
public class M001Controller {

    private WebResource service;

    public M001Controller() {
        ClientConfig config = new DefaultClientConfig();
        Client client = Client.create(config);
        config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
        service = client.resource(UriBuilder.fromUri("http://localhost:8080/EPrescribing/rest").build());
    }

    public Short login(String username, String password) {
        MultivaluedMap formData = new MultivaluedMapImpl();
        formData.add("username", username);
        formData.add("password", password);
        ClientResponse clientResponse = service.path("bl001").path("login").
                post(ClientResponse.class, formData);
        return Short.parseShort(clientResponse.getEntity(String.class));
    }

    public void logout(IModule module) {
        ((WebApplicationContext) module.getApplication().getContext()).getHttpSession().invalidate();
    }

    public T001User getUserFromUsername(String username) {
        T001User user = service.path("bl001").path("user").queryParam("username", username).
                get(T001User.class);
        return user;
    }

    public BeanItem<T001User> getRegisterFormBeanItem(String username) {
        if (username == null) {
            return new BeanItem<T001User>(new T001User());
        } else {
            return new BeanItem<T001User>(getUserFromUsername(username));
        }
    }

    public BeanItem<T002Doctor> getDoctorFormBeanItem(Item userItem) {
        if (userItem != null) {
            T001User user = (T001User) ((BeanItem) userItem).getBean();
            Collection<T002Doctor> doctors = user.getT002DoctorCollection();
            if (doctors != null && !doctors.isEmpty()) {
                return new BeanItem<T002Doctor>(doctors.iterator().next());
            }
        }
        return new BeanItem<T002Doctor>(new T002Doctor());
    }

    public BeanItem<T003Pharmacist> getPharmacistFormBeanItem(Item userItem) {
        if (userItem != null) {
            T001User user = (T001User) ((BeanItem) userItem).getBean();
            Collection<T003Pharmacist> pharmacists = user.getT003PharmacistCollection();
            if (pharmacists != null && !pharmacists.isEmpty()) {
                return new BeanItem<T003Pharmacist>(pharmacists.iterator().next());
            }
        }
        return new BeanItem<T003Pharmacist>(new T003Pharmacist());
    }

    public Collection<T004Insurance> getInsurances() {
        Collection<T004Insurance> insurances = service.path("bl001").path("insurances")
                .get(new GenericType<Collection<T004Insurance>>() {
        });
        return insurances;
    }

    public Boolean registerPharmacist(Item userItem, Item pharmacistItem) {
        T001User user = (T001User) ((BeanItem) userItem).getBean();
        T003Pharmacist pharmacist = (T003Pharmacist) ((BeanItem) pharmacistItem).getBean();
        RegisterPharmacistBean bean = new RegisterPharmacistBean(user, pharmacist);
        ClientResponse clientResponse = service.path("bl001").path("rpharm").
                accept(MediaType.TEXT_PLAIN).post(
                ClientResponse.class, new GenericEntity<RegisterPharmacistBean>(bean, RegisterPharmacistBean.class));
        String response = clientResponse.getEntity(String.class);
        return response.equals("0");
    }

    public Boolean registerDoctor(Item userItem, Item doctorItem, Collection insurances) {
        T001User user = (T001User) ((BeanItem) userItem).getBean();
        T002Doctor doctor = (T002Doctor) ((BeanItem) doctorItem).getBean();
        RegisterDoctorBean bean = new RegisterDoctorBean(user, doctor, insurances);
        ClientResponse clientResponse = service.path("bl001").path("rdoctor").
                accept(MediaType.TEXT_PLAIN).post(
                ClientResponse.class, new GenericEntity<RegisterDoctorBean>(bean, RegisterDoctorBean.class));
        String response = clientResponse.getEntity(String.class);
        return response.equals("0");
    }
}
